This article contains multiple example strategies which trade according to cross of two indicator lines. The first two examples work with any feed but the tick feed - one simply chooses the feed type and parameters in strategy parameter dialog on strategy start. The third example works only with time period aggregation feed.<br/><div class="dw_article_headers_list"><div class="dw_article_header_h1"><a href="#SMA_Crossover/Any_feed">Any feed</a></div><div class="dw_article_header_h2"><a href="#SMA_Crossover/Visual_version">Visual version</a></div><div class="dw_article_header_h1"><a href="#SMA_Crossover/Time_period_aggregation_feed">Time period aggregation feed</a></div></div><span id="SMA_Crossover/Any_feed"><h1>Any feed</h1></span>Consider a strategy which trades according to SMA cross direction. On fast SMA going below slow SMA it buys, on the opposite cross - sells.<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jforex.strategies.sdk</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.*</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IEngine.OrderCommand</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IIndicators.AppliedPrice</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedDescriptor</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedListener</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor</span><span style="color: #339933;">;</span><br /><br /><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />&nbsp;* The strategy trades according to SMA trend.<br />&nbsp;* On up-trend it buys and on down-trend - sells.<br />&nbsp;*/</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SmaTrendStrategy <span style="color: #000000; font-weight: bold;">implements</span> IStrategy, IFeedListener <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Feed&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IFeedDescriptor feedDescriptor <span style="color: #339933;">=</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> TimePeriodAggregationFeedDescriptor<span style="color: #009900;">&#40;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURUSD</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Period.<span style="color: #006633;">TEN_SECS</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OfferSide.<span style="color: #006633;">ASK</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Filter.<span style="color: #006633;">NO_FILTER</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Amount&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> amount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.001</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Stop loss&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> slPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Take profit&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> tpPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span>, description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;close the existing order on creation of a new order if it has not been closed yet&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> closePreviousOrder <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> smaTimePeriod <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> LAST <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> PREV <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> SCND_TO_LAST <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IEngine engine<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IHistory history<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IConsole console<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IOrder order<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IIndicators indicators<span style="color: #339933;">;</span><br />&nbsp; <br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// subscribe the instrument that we are going to work with</span><br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Collections</span>.<span style="color: #006633;">singleton</span><span style="color: #009900;">&#40;</span>feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>feedDescriptor.<span style="color: #006633;">getDataType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> DataType.<span style="color: #006633;">TICKS</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getWarn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The strategy can't trade according to the tick feed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">subscribeToFeed</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; IBar prevFeedData <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IBar<span style="color: #009900;">&#41;</span> history.<span style="color: #006633;">getFeedData</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span>prevFeedData.<span style="color: #006633;">getClose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> prevFeedData.<span style="color: #006633;">getOpen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFeedData<span style="color: #009900;">&#40;</span>IFeedDescriptor feedDescriptor, ITimedData feedData<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//we need 3 last indicator values to detect the trend change</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> sma <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>feedDescriptor, AppliedPrice.<span style="color: #006633;">CLOSE</span>, feedDescriptor.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, smaTimePeriod<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, feedData.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>SCND_TO_LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> sma<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// down trend</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> sma<span style="color: #009900;">&#91;</span>SCND_TO_LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> sma<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> sma<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// up trend</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getErr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>IMessage message<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> submitOrder<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> isLong<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> slPrice, tpPrice<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Instrument</span> instrument <span style="color: #339933;">=</span> feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; ITick lastTick <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>instrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; OrderCommand orderCmd <span style="color: #339933;">=</span> isLong <span style="color: #339933;">?</span> OrderCommand.<span style="color: #006633;">BUY</span> <span style="color: #339933;">:</span> OrderCommand.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Calculating stop loss and take profit prices</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isLong<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> slPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> tpPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> slPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> tpPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>closePreviousOrder <span style="color: #339933;">&amp;&amp;</span> order <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">FILLED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//we don't use order.waitForUpdate, since our next actions don't depend on the previous order anymore</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;trend_&quot;</span><span style="color: #339933;">+</span>orderCmd.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, instrument, orderCmd, amount, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">20</span>, slPrice, tpPrice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">FILLED</span> <span style="color: #339933;">||</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">OPENED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTick<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, ITick tick<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccount<span style="color: #009900;">&#40;</span>IAccount account<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br /><span style="color: #009900;">&#125;</span><br /></div><a href="https://www.dukascopy.com/wiki/https://www.dukascopy.com/wiki/files/SmaCrossStrategy.java">SmaCrossStrategy.java</a><br/><span id="SMA_Crossover/Visual_version"><h2>Visual version</h2></span>Consider adding a couple of visual features to the previous strategy. That is, a chart of the chosen feed gets opened and on that chart the correct indicators get plotted. Moreover, an OHLC informer object gets added to the chart and we configure it such that it shows the indicator values. And finally we add a signal chart object on every cross of the indicator lines.<br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jforex.strategies.sdk</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Color</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Font</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.*</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IEngine.OrderCommand</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IIndicators.AppliedPrice</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IChartDependentChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IChartObjectFactory</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.drawings.IOhlcChartObject</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedDescriptor</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.IFeedListener</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle</span><span style="color: #339933;">;</span><br /><br /><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />&nbsp;* The strategy trades according to SMA cross direction.<br />&nbsp;* On fast SMA going below slow SMA it buys, on the opposite cross - sells.<br />&nbsp;* <br />&nbsp;* The strategy itself opens a chart (if no such chart has been opened yet)<br />&nbsp;* and adds the indicator values to the OHLC index.<br />&nbsp;*/</span><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SmaCrossStrategyVisual <span style="color: #000000; font-weight: bold;">implements</span> IStrategy, IFeedListener <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Feed&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IFeedDescriptor feedDescriptor <span style="color: #339933;">=</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> TimePeriodAggregationFeedDescriptor<span style="color: #009900;">&#40;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURUSD</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Period.<span style="color: #006633;">TEN_SECS</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OfferSide.<span style="color: #006633;">ASK</span>, <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Filter.<span style="color: #006633;">NO_FILTER</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Amount&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> amount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.001</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Stop loss&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> slPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Take profit&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> tpPips <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span>, description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;close the existing order on creation of a new order if it has not been closed yet&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> closePreviousOrder <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> smaTimePeriodFast <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> smaTimePeriodSlow <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Color</span> fastColor <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Color</span> slowColor <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> LAST <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> PREV <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IEngine engine<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IHistory history<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IConsole console<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IContext context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IOrder order<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IIndicators indicators<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IChart chart<span style="color: #339933;">;</span><br />&nbsp; <br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// subscribe the instrument that we are going to work with</span><br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">setSubscribedInstruments</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Collections</span>.<span style="color: #006633;">singleton</span><span style="color: #009900;">&#40;</span>feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>feedDescriptor.<span style="color: #006633;">getDataType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> DataType.<span style="color: #006633;">TICKS</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getWarn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The strategy can't trade according to the tick feed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">subscribeToFeed</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; setupChart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; IBar prevFeedData <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IBar<span style="color: #009900;">&#41;</span> history.<span style="color: #006633;">getFeedData</span><span style="color: #009900;">&#40;</span>feedDescriptor, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span>prevFeedData.<span style="color: #006633;">getClose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> prevFeedData.<span style="color: #006633;">getOpen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setupChart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IChart c <span style="color: #339933;">:</span> context.<span style="color: #006633;">getCharts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getFeedDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>feedDescriptor<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart <span style="color: #339933;">=</span> c<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//no such chart opened yet - we open it now</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>chart <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart <span style="color: #339933;">=</span> context.<span style="color: #006633;">openChart</span><span style="color: #009900;">&#40;</span>feedDescriptor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>indicators.<span style="color: #006633;">getIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> smaTimePeriodFast <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Color</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> fastColor <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> DrawingStyle<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> DrawingStyle.<span style="color: #006633;">LINE</span> <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>indicators.<span style="color: #006633;">getIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> smaTimePeriodSlow <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Color</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> slowColor <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> DrawingStyle<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> DrawingStyle.<span style="color: #006633;">LINE</span> <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; IOhlcChartObject ohlc <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IChartObject obj <span style="color: #339933;">:</span> chart.<span style="color: #006633;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> IOhlcChartObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IOhlcChartObject<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ohlc <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ohlc <span style="color: #339933;">=</span> chart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createOhlcInformer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ohlc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; ohlc.<span style="color: #006633;">setShowIndicatorInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> addSignal<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> isLong, IBar previousBar<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IChartObjectFactory factory <span style="color: #339933;">=</span> chart.<span style="color: #006633;">getChartObjectFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Instrument</span> instrument <span style="color: #339933;">=</span> feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IChartDependentChartObject signal <span style="color: #339933;">=</span> isLong <span style="color: #339933;">?</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.<span style="color: #006633;">createSignalUp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalUp_&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, previousBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, previousBar.<span style="color: #006633;">getLow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">:</span> factory.<span style="color: #006633;">createSignalDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;signalDownKey_&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, previousBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, previousBar.<span style="color: #006633;">getHigh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA cross&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Monospaced&quot;</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">BOLD</span>, <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>isLong <span style="color: #339933;">?</span> fastColor <span style="color: #339933;">:</span> slowColor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; signal.<span style="color: #006633;">setStickToCandleTimeEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; chart.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>signal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFeedData<span style="color: #009900;">&#40;</span>IFeedDescriptor feedDescriptor, ITimedData feedData<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> time <span style="color: #339933;">=</span> feedData.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> smaFast <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>feedDescriptor, AppliedPrice.<span style="color: #006633;">CLOSE</span>, feedDescriptor.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, smaTimePeriodFast<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, time, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> smaSlow <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>feedDescriptor, AppliedPrice.<span style="color: #006633;">CLOSE</span>, feedDescriptor.<span style="color: #006633;">getOfferSide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, smaTimePeriodSlow<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, time, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>smaFast<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> smaFast<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> smaFast<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> smaSlow<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> smaFast<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> smaSlow<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// smaFast falls below smaSlow</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addSignal<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span>IBar<span style="color: #009900;">&#41;</span> feedData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>smaFast<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> smaFast<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> smaFast<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> smaSlow<span style="color: #009900;">&#91;</span>LAST<span style="color: #009900;">&#93;</span> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> smaFast<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> smaSlow<span style="color: #009900;">&#91;</span>PREV<span style="color: #009900;">&#93;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// smaFast overtakes smaSlow</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitOrder<span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addSignal<span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span>IBar<span style="color: #009900;">&#41;</span> feedData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getErr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>IMessage message<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> submitOrder<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> isLong<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> slPrice, tpPrice<span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Instrument</span> instrument <span style="color: #339933;">=</span> feedDescriptor.<span style="color: #006633;">getInstrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; ITick lastTick <span style="color: #339933;">=</span> history.<span style="color: #006633;">getLastTick</span><span style="color: #009900;">&#40;</span>instrument<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; OrderCommand orderCmd <span style="color: #339933;">=</span> isLong <span style="color: #339933;">?</span> OrderCommand.<span style="color: #006633;">BUY</span> <span style="color: #339933;">:</span> OrderCommand.<span style="color: #006633;">SELL</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Calculating stop loss and take profit prices</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isLong<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> slPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getAsk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> tpPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> slPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpPrice <span style="color: #339933;">=</span> lastTick.<span style="color: #006633;">getBid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> tpPips <span style="color: #339933;">*</span> instrument.<span style="color: #006633;">getPipValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>closePreviousOrder <span style="color: #339933;">&amp;&amp;</span> order <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">FILLED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//we don't use order.waitForUpdate, since our next actions don't depend on the previous order anymore</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cross_&quot;</span><span style="color: #339933;">+</span>orderCmd.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, instrument, orderCmd, amount, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">20</span>, slPrice, tpPrice<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">FILLED</span> <span style="color: #339933;">||</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">OPENED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTick<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, ITick tick<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccount<span style="color: #009900;">&#40;</span>IAccount account<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br /><span style="color: #009900;">&#125;</span><br /></div><a href="https://www.dukascopy.com/wiki/https://www.dukascopy.com/wiki/files/SmaCrossStrategyVisual.java">SmaCrossStrategyVisual.java</a><br/><span id="SMA_Crossover/Time_period_aggregation_feed"><h1>Time period aggregation feed</h1></span>The <i>SMAStrategy</i> strategy based on two SMA (Simple moving average) : sma with time period 10 and  sma with time period 90. The strategy logic can be described as 2 actions:<br/><ul><li>If Sma 10 cross Sma 90 from <b>up to down</b>, then the strategy closes existing <b>short</b> position and open a buy order if <b>long</b> position does not exist</li><li>If Sma 10 cross Sma 90 from <b>down to up</b>, then the strategy closes existing <b>long</b> position and open a sell order if <b>short</b> position does not exist</li></ul>The picture below shows two SMA crossover <i>blue line</i>: SMA 10; and yellow line: Sma 90:<br/><img src="https://www.dukascopy.com/wiki/files//Sma_crossover.JPG" border="0" /><br/><div class="dw_article_geshi_code"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.*</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IEngine.OrderCommand</span><span style="color: #339933;">;</span><br /><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dukascopy.api.IIndicators.AppliedPrice</span><span style="color: #339933;">;</span><br /><br /><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SMAStrategy <span style="color: #000000; font-weight: bold;">implements</span> IStrategy <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IEngine engine<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IConsole console<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IHistory history<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IIndicators indicators<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> counter <span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div>The filteredSma90 and filteredSma10 arrays first element (index <b>0</b> ) contains a <i>next-to-last  sma</i> value and second element  (index <b>1</b> ) contains a<i> last candle sma</i> value.<br/><div class="dw_article_geshi_code">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> filteredSma90<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// slow SMA</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> filteredSma10<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// fast SMA</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IOrder order <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br /></div>The strategy contains 3 configurable parameters: trade instrument, indicator period and indicator filters.<br/><div class="dw_article_geshi_code">&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Instrument&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Instrument</span> selectedInstrument <span style="color: #339933;">=</span> <span style="color: #003399;">Instrument</span>.<span style="color: #006633;">EURUSD</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Period&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Period selectedPeriod <span style="color: #339933;">=</span> Period.<span style="color: #006633;">THIRTY_MINS</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; @Configurable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SMA filter&quot;</span><span style="color: #009900;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Filter indicatorFilter <span style="color: #339933;">=</span> Filter.<span style="color: #006633;">NO_FILTER</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>IContext context<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">engine</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getEngine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">console</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getConsole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">history</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicators</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getIndicators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccount<span style="color: #009900;">&#40;</span>IAccount account<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>IMessage message<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /></div>Method <i>onStop</i> close all created orders.<br/><div class="dw_article_geshi_code">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IOrder order <span style="color: #339933;">:</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">getOrder</span><span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /></div><br/>Method <i>onTick</i> contains all trade logic.<br/><div class="dw_article_geshi_code">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTick<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, ITick tick<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>instrument.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>selectedInstrument<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; IBar prevBar <span style="color: #339933;">=</span> history.<span style="color: #006633;">getBar</span><span style="color: #009900;">&#40;</span>instrument, selectedPeriod, OfferSide.<span style="color: #006633;">BID</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; filteredSma90 <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>instrument, selectedPeriod, OfferSide.<span style="color: #006633;">BID</span>, AppliedPrice.<span style="color: #006633;">CLOSE</span>, <span style="color: #cc66cc;">90</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indicatorFilter, <span style="color: #cc66cc;">2</span>, prevBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; filteredSma10 <span style="color: #339933;">=</span> indicators.<span style="color: #006633;">sma</span><span style="color: #009900;">&#40;</span>instrument, selectedPeriod, OfferSide.<span style="color: #006633;">BID</span>, AppliedPrice.<span style="color: #006633;">CLOSE</span>, <span style="color: #cc66cc;">10</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indicatorFilter, <span style="color: #cc66cc;">2</span>, prevBar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div>SMA10 crossover SMA90 from UP to DOWN ,  the strategy close existing long position and create a sell order if sell order not created before. <br/><div class="dw_article_geshi_code">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> filteredSma90<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> filteredSma90<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IOrder orderInMarket <span style="color: #339933;">:</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>orderInMarket.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Closing Long position&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderInMarket.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">CLOSED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span>getLabel<span style="color: #009900;">&#40;</span>instrument<span style="color: #009900;">&#41;</span>, instrument, OrderCommand.<span style="color: #006633;">SELL</span>, <span style="color: #cc66cc;">0.01</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div>SMA10 crossover SMA90 from DOWN to UP,  the strategy close existing short position and create a buy order if buy order not created before.<br/><div class="dw_article_geshi_code">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> filteredSma90<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>filteredSma10<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> filteredSma90<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IOrder orderInMarket <span style="color: #339933;">:</span> engine.<span style="color: #006633;">getOrders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>orderInMarket.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Closing Short position&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderInMarket.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span> not order.<span style="color: #006633;">isLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>IOrder.<span style="color: #006633;">State</span>.<span style="color: #006633;">CLOSED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> engine.<span style="color: #006633;">submitOrder</span><span style="color: #009900;">&#40;</span>getLabel<span style="color: #009900;">&#40;</span>instrument<span style="color: #009900;">&#41;</span>, instrument, OrderCommand.<span style="color: #006633;">BUY</span>, <span style="color: #cc66cc;">0.01</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp;<br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument, Period period, IBar askBar, IBar bidBar<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JFException <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getLabel<span style="color: #009900;">&#40;</span><span style="color: #003399;">Instrument</span> instrument<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> label <span style="color: #339933;">=</span> instrument.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; label <span style="color: #339933;">=</span> label <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>counter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; label <span style="color: #339933;">=</span> label.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> label<span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br />&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; console.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /><br /><span style="color: #009900;">&#125;</span></div><a href="https://www.dukascopy.com/wiki/files//SMAStrategy.java">SMAStrategy.java</a>